#ifndef RINEX_PARSER_H
#define RINEX_PARSER_H

#include "Common.h"
#include <string>
#include <vector>
#include <map>
#include <memory>

// Forward declarations
class SP3Interpolator;
class GPSSatelliteOrbit;

// Define RinexVersion enum
enum class RinexVersion {
    Unknown,
    V2x,
    V3x
};

// Define RinexFileType enum
enum class RinexFileType {
    Unknown,
    Observation,
    Navigation
};

// Define ObservationType struct
struct ObservationType {
    std::string system;
    std::string code;
    double frequency;
};

// Define EpochTime struct
struct EpochTime {
    int year;
    int month;
    int day;
    int hour;
    int minute;
    double second;
};

class RinexParser {
public:
    RinexParser();
    ~RinexParser();
    
    // Basic file parsing methods
    bool parseObservationFile(const std::string& filename);
    bool parseObservationFile(const std::string& filename, const std::vector<std::string>& enabledSystems);
    bool parseNavigationFile(const std::string& filename);
    bool parseSP3File(const std::string& filename);
    
    // Multiple file parsing methods
    bool parseMultipleObservationFiles(const std::vector<std::string>& filenames);
    bool parseMultipleObservationFiles(const std::vector<std::pair<std::string, std::vector<std::string>>>& fileConfigs);
    bool parseMultipleNavigationFiles(const std::vector<std::string>& filenames);
    bool parseMultipleSP3Files(const std::vector<std::string>& filenames);
    
    // Data access methods
    std::vector<GNSSObservation> getObservations() const;
    std::vector<GNSSObservation> getObservationsBySystem(const std::string& system) const;
    std::vector<GNSSObservation> getObservationsBySystems(const std::vector<std::string>& systems) const;
    StationInfo getStationInfo() const;
    std::vector<double> getStationPosition(const GNSSObservation& obs);
    
    // Configuration methods
    void useHighPrecisionOrbit(bool use);
    bool isUsingHighPrecisionOrbit() const;
    void setMinElevation(double minElevation);
    
private:
    // Private data members
    std::vector<GNSSObservation> observations_;
    std::map<std::string, std::vector<double>> navData_;
    std::map<std::string, GPSSatelliteOrbit> satelliteOrbits_;
    std::unique_ptr<SP3Interpolator> sp3Interpolator_;
    StationInfo stationInfo_;
    bool usePreciseOrbit_ = false;
    std::vector<std::string> enabledSystems_; // 存储启用的GNSS系统
    std::vector<std::string> enabledGnssSystems_;
    RinexVersion obsVersion_ = RinexVersion::Unknown;
    RinexVersion version_ = RinexVersion::Unknown; // 当前解析文件的版本
    RinexFileType fileType_ = RinexFileType::Unknown; // 当前解析文件的类型
    int versionMajor_ = 0; // 主版本号
    int versionMinor_ = 0; // 次版本号
    long headerEndPos_ = -1; // 头部结束位置
    double minElevation_ = 0.0; // 最小高度角阈值
    EpochTime firstObsTime_; // 第一个观测时间
    
    std::vector<ObservationType> obsTypes_; // 观测类型
    std::map<int, double> obsTypeMap_; // 观测类型映射
    std::map<std::string, std::vector<std::string>> obsTypesBySys_; // 按系统存储的观测类型
    
    // Internal helper methods
    RinexVersion detectRinexVersion(const std::string& filename);
    void parseHeader(std::ifstream& file);
    void parseObservationRecord(std::ifstream& file);
    void parseNavigationRecord(std::ifstream& file);
    void computeSatelliteGeometry();
    void calculateElevationAndAzimuth(GNSSObservation& obs, const std::vector<double>& satPos);
    std::vector<double> calculateSatellitePosition(const std::string& satId, double time);
    std::vector<double> calculateBroadcastPosition(const std::string& satId, double time);
    std::vector<double> calculatePrecisePosition(const std::string& satId, double time);
    void initializeSatelliteOrbits();
    bool parseRinex2ObsFile(const std::string& filename);
    bool parseRinex3ObsFile(const std::string& filename);
    void parseRinex3ObsTypes(const std::string& line);
    void extractDualFrequencyObs(GNSSObservation& obs);
};

#endif // RINEX_PARSER_H